package com.whjfxx.terminal.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by b.c on 2022/6/23 15:15
 */
public class AddressUtil1 {

    /**
     * 获取地理位置名称
     *
     * param location
     *            经纬度坐标,经度在前,纬度在后，经纬度间以“,”分割
     * return
     */
    public static String getAddressNameByGeocoder1(String lon, String lat) {
        String jsonString = sendGet(getRegeoUrl(lon, lat));
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        String addressName = jsonObject.getJSONObject("result").getString("formatted_address");
        return addressName;
    }


    /**
     * 向指定URL发送GET方法的请求
     *
     * param url
     *            发送请求的URL
     * return URL 所代表远程资源的响应结果
     */
    public static String sendGet(String url) {
        System.out.println(url);
        String resp = HttpUtil.get(url);
        System.out.println(resp);
        return resp;


//        String result = "";
//        BufferedReader in = null;
//        try {
//            String urlNameString = url;
//            URL realUrl = new URL(urlNameString);
//            // 打开和URL之间的连接
//            URLConnection connection = realUrl.openConnection();
//            // 设置通用的请求属性
//
//            connection.setRequestProperty("connection", "Keep-Alive");
//            connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
//            connection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
//            connection.setRequestProperty("Cache-Control", "no-cache");
//            connection.setRequestProperty("Access-Control-Allow-Origin", "*");
//            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.39");
//            // 建立实际的连接
//            connection.connect();
//            // 定义 BufferedReader输入流来读取URL的响应
//            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
//            String line;
//            while ((line = in.readLine()) != null) {
//                result += line;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        } finally { // 使用finally块来关闭输入流
//            try {
//                if (in != null) {
//                    in.close();
//                }
//            } catch (Exception e2) {
//                e2.printStackTrace();
//            }
//        }
//        return result;
    }

    /**
     * 逆地理编码url
     *
     * param location
     *            经纬度坐标,经度在前,纬度在后，经纬度间以“,”分割
     * return
     */
    public static String getRegeoUrl(String lon, String lat) {
        return "http://api.tianditu.gov.cn/geocoder?postStr=" +
                "{\"lon\":" + lon + ",\"lat\":" + lat + ",\"ver\":1}" +
                "&type=geocode&tk=483eb2461c7313c2f7b6bfd753a240a0";
    }

    public static void main(String[] args) {

        System.out.println(getAddressNameByGeocoder1("114.24921","30.57733"));

    }

}
